package com.project.demo.controller.edu;


import com.project.demo.entity.param.EduCourseProgressParam;
import com.project.demo.entity.param.EduCourseSearchParam;
import com.project.demo.entity.vo.EduCourseDetailVO;
import com.project.demo.entity.vo.R;
import com.project.demo.entity.vo.teacher.EduChapterVO;
import com.project.demo.entity.vo.teacher.EduVideoVO;
import com.project.demo.enums.CourseStatusEnum;
import com.project.demo.service.edu.EduCourseProgressService;
import com.project.demo.service.edu.EduOnlineService;
import com.project.demo.service.edu.EduVideoService;
import com.project.demo.util.RUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;
import java.util.Objects;


@Slf4j
@RestController
@RequestMapping("/edu")
public class EduOnlineController {

	@Autowired
	EduOnlineService eduOnlineService;
	@Autowired
	EduVideoService eduVideoService;
	@Autowired
	EduCourseProgressService eduCourseProgressService;


	@GetMapping("/course/draft")
	public R getDraft(Integer userId) throws IOException {
		return eduOnlineService.getDraft(userId);
	}


	@PostMapping("/course/create")
	@ApiOperation("创建课程")
	public R createCourse(EduCourseDetailVO vo,
	                      @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
		if (Objects.isNull(file)) {
			return RUtils.fail("课程封面不能为空");
		}
		// 设置课程状态为草稿
		vo.setStatus(CourseStatusEnum.DRAFT);
		return eduOnlineService.createCourse(vo, file);
	}

	@GetMapping("/chapter/list/{courseId}")
	@ApiOperation("获取章节")
	public R list(@PathVariable("courseId") @Min(1) Integer courseId) {
		List<EduChapterVO> list = eduOnlineService.listChapters(courseId);
		return RUtils.success("章节列表信息", list);
	}

	@PostMapping("/chapter/create")
	@ApiOperation("创建章节")
	public R create(@RequestBody EduChapterVO vo) {
		return eduOnlineService.create(vo);
	}

	@PostMapping("/chapter/update")
	@ApiOperation("更新章节信息")
	public R update(@RequestBody EduChapterVO vo) {
		return eduOnlineService.update(vo);
	}

	@PostMapping("/chapter/delete/{id}")
	@ApiOperation("删除章节")
	public R delete(@PathVariable("id") Integer id) {
		return eduOnlineService.delete(id);
	}


	@PostMapping("/course/submit/{id}")
	@ApiOperation("提交审核课程")
	public R courseSubmit(@PathVariable("id") @Min(1) Integer id) {
		boolean b = eduOnlineService.changeStatus(id, CourseStatusEnum.FIRST_AUDITING);
		return RUtils.commonFailOrNot(b ? 1 : 0, "提交审核");
	}

	@PostMapping("/course/list")
	@ApiOperation("搜索课程")
	public R courseList(@RequestBody EduCourseSearchParam param) {
		return eduOnlineService.courseList(param);
	}


	@PostMapping("/content/get/courses")
	@ApiOperation("获取所有课程")
	public R getCourses(@RequestBody @Valid EduCourseSearchParam param) {
		param.setPageSize(12);
		return eduOnlineService.listForApp(param);
	}

	@GetMapping("/content/courses/{id}")
	@ApiOperation("获取课程详情")
	public R getCourseDetail(@PathVariable("id") @Min(1) Integer id) {
		return eduOnlineService.getDetails(id);
	}

	@GetMapping("/get/videos/{chapterId}")
	@ApiOperation("获取章节视频")
	public R getChapterVideos(@PathVariable("chapterId") @Min(1) Integer chapterId) {
		List<EduVideoVO> list = eduOnlineService.listVideos(chapterId);
		return RUtils.success("章节视频列表信息", list);
	}


	@PostMapping("/get/video/auth")
	@ApiOperation("获取视频播放凭证")
	public R getVideoPlayAuth(@RequestParam("courseId") @NotNull @Min(1) Integer courseId,
	                          @RequestParam("videoId") @NotNull @Min(1) Integer videoId,
	                          @RequestParam("videoSourceId") @NotEmpty String videoSourceId) {
		R r = eduVideoService.getVideoPlayAuth(courseId, videoId, videoSourceId);

		return r;
	}

	@GetMapping("/get/progress")
	@ApiOperation("获取课程学习进度")
	public R getChapterVideos(EduCourseProgressParam param) {
		return eduCourseProgressService.getChapterVideos(param);
	}

	@PostMapping("/save/progress")
	@ApiOperation("保存课程学习进度")
	public R saveChapterVideos(@RequestBody EduCourseProgressParam param) {
		return eduCourseProgressService.saveChapterVideos(param);
	}


}
